﻿using log4net;
using log4net.Config;
using MySql.Data.MySqlClient;
using NewChillers.Helpers;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;

namespace NewChillers
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            // 初始化日志
            InitializeLogging();

            base.OnStartup(e);
        }

        /// <summary>
        /// 初始化日志
        /// </summary>
        private void InitializeLogging()
        {
            try
            {
                // 启用log4net内部调试
                log4net.Util.LogLog.InternalDebugging = true;

                var configFile = new FileInfo("Configs/log4net.config");
                if (configFile.Exists)
                {
                    XmlConfigurator.ConfigureAndWatch(configFile);
                    Debug.WriteLine("log4net 配置成功");

                    // 测试数据库连接
                    TestDatabaseConnection();
                }
                else
                {
                    Debug.WriteLine("找不到 log4net.config 文件");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"log4net 初始化失败: {ex.Message}");
            }
        }

        private void TestDatabaseConnection()
        {
            try
            {
                using (var connection = new MySqlConnection("server=localhost;database=new_chillers;uid=root;pwd=ROOT;port=3306;"))
                {
                    connection.Open();
                    Debug.WriteLine("数据库 连接测试成功");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"数据库 连接测试失败: {ex.Message}");
            }
        }
    }
}
